<template>
  <div class="user-list-container">
    <el-form
      :model="form"
      :rules="rules"
      ref="formRef"
      label-width="150px"
      class="demo-form"
    >
      <el-form-item label="课程亮点" prop="brand_teacher_data">
        <el-button type="primary" plain @click="pp" v-if="infoList.length<4"
          >添加</el-button
        ><br />
      </el-form-item>
      <div
        style="
          display: flex;
          flex-direction: column;
          width: 500px;
          margin-left: 200px;
          margin-bottom: 100px;
        "
        v-if="form.brand_highlights"
      >
        <div style="display: flex;" 
            v-for="(item, index) in infoList">
          <el-input
            v-model="infoList[index]"
          />
          <el-button type="error" @click="del(index)">删除</el-button>
        </div>
      </div>
      <el-form-item label="推荐老师" prop="brand_teacher_data">
        <el-select
          v-model="form.brand_teacher_data"
          placeholder="请选择老师"
          multiple
          filterable
        >
          <el-option
            v-for="item in teacherList"
            :key="item.teacher_id"
            :label="item.teacher_name"
            :value="item.teacher_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="公开课" prop="brand_course_data">
        <el-select
          v-model="form.brand_course_data"
          placeholder="请选择公开课"
          multiple
          filterable
        >
          <el-option
            v-for="item in classList"
            :key="item.course_cate_id"
            :label="item.course_name"
            :value="item.course_cate_id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm"> 保存 </el-button>
    </div>
  </div>
</template>

<script>
import { Plus } from "@element-plus/icons-vue";
import api from "@/request/api.js";
import upload from "@/components/upload.vue";

export default {
  name: "UserList",
  components: { upload },
  data() {
    return {
      form: { brand_course_data: [], brand_teacher_data: [] },
      teacherList: [],
      classList: [],
      infoList: [],
    };
  },
  mounted() {
    api.teacher.getList({ size: 999 }).then((res) => {
      if (res.data.code == 200) {
        this.teacherList = res.data.content.data;
      }
    });
    api.classaa.getList({ size: 999 }).then((res) => {
      if (res.data.code == 200) {
        this.classList = res.data.content.data;
      }
    });
    api.config.getBrandInfo().then((res) => {
      if (res.data.code == 200) {
        this.form = res.data.content.data;
        this.infoList = this.form.brand_highlights.split(",");
            const arr1 = []
            for (let index = 0; index < this.form.brand_teacher_data.length; index++) {
                const element = this.form.brand_teacher_data[index];
                arr1.push(element.teacher_id)
            } const arr2 = []
            for (let index = 0; index < this.form.brand_course_data.length; index++) {
                const element = this.form.brand_course_data[index];
                arr2.push(element.course_id)
            }
        this.form.brand_teacher_data = arr1;
        this.form.brand_course_data = arr2;
        console.log("form", this.infoList);
      }
    });
  },
  methods: {
    pp(){
        this.infoList.push('')
    },
    del(index) {
      this.infoList.splice(index, 1);
    },
    submitForm() {
      this.form.brand_highlights = this.infoList.join(",");
      api.config
        .brandInfoSave({
          ...this.form,
        })
        .then((res) => {
          if (res?.status == 200) {
            this.$message.success("保存成功");
          }
        });
    },
    resetForm() {
      this.$refs.formRef.resetFields();
    },
    //选择地点
    handleRegionChange(val) {
      this.form.province_id = val.province;
      this.form.city_id = val.city;
      this.form.area_id = val.district;
    },
  },
};
</script>

<style scoped>
.user-list-container {
  padding: 20px;
}
.search-card {
  margin-bottom: 20px;
}
.table-card {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
  text-align: right;
}
</style>
